<template>
	<view>
		<view style="text-align: center;">
			<img height="220" 
			:src="goodsInfo.cover_url">
		</view>
		<view class="u-p-t-10 u-p-l-20 top2" style="margin-top: -10px;">
			<view class="title u-line-2">{{goodsInfo.title}}</view>
		</view>
		<view class="u-p-b-40  des">
			<view class="price">￥{{goodsInfo.price}}</view>
			<view class="sales">销量: {{goodsInfo.sales}}</view>
		</view>
		<view>
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
		</view>
		
		<view v-if="current==0">
			<view class="info u-p-b-80">
				<u-parse :html="goodsInfo.details"></u-parse>
			</view>
		</view>
		
		<view v-if="current==1">
			<view class="comment" v-for="(res, index) in commentList" :key="res.id">
				<view class="left"><image :src="res.user.avatar_url" mode="aspectFill"></image></view>
				<view class="right">
					<view class="top">
						<view class="name">{{ res.user.name }}</view>
						<!-- <view class="like" :class="{ highlight: res.isLike }">
							<view class="num">{{ res.likeNum }}</view>
							<u-icon v-if="!res.isLike" name="thumb-up" :size="30" color="#9a9a9a" @click="getLike(index)"></u-icon>
							<u-icon v-if="res.isLike" name="thumb-up-fill" :size="30" @click="getLike(index)"></u-icon>
						</view> -->
					</view>
					<view class="content">{{ res.content }}</view>
					<!-- <view class="reply-box">
						<view class="item" v-for="(item, index) in res.replyList" :key="item.index">
							<view class="username">{{ item.name }}</view>
							<view class="text">{{ item.contentStr }}</view>
						</view>
						<view class="all-reply" @tap="toAllReply" v-if="res.replyList != undefined">
							共{{ res.allReply }}条回复
							<u-icon class="more" name="arrow-right" :size="26"></u-icon>
						</view>
					</view> -->
					<view class="bottom">
						{{ res.created_at }}
						<!-- <view class="reply">回复</view> -->
					</view>
				</view>
			</view>
		</view>
		
		<view class="u-p-b-80" v-if="current == 2">
			<u-row gutter="16">
				<u-col v-for="goods in goodsList" span="6">
					<goods-card :goods="goods"></goods-card>
				</u-col>
			</u-row>
		</view>
		
		<view class="navigation" style="position: fixed;bottom: 0;width: 100%;display: flex;justify-content: center;">
			<view class="left">
				<view class="item u-text-center" @click="collect">
					<block v-if="isCollect==0">
						<u-icon name="star" :size="40" color="black"></u-icon>
						<view class="text u-line-1 ">收藏</view>
					</block>
					<block v-else>
						<u-icon name="star" :size="40" color="red"></u-icon>
						<view class="text u-line-1 " style="color: red;" >已收藏</view>
					</block>
				</view>
				<view class="item car" >
					<u-badge class="car-num" :count="cartCount" type='error' :offset="[-3,-6]"></u-badge>
					<u-icon name="shopping-cart" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1" >购物车</view>
				</view>
			</view>
			<view class="right">
				<view class="cart btn u-line-1" @click="addCarts">加入购物车</view>
				<view class="buy btn u-line-1">立即购买</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[{
					name: '商品详情'
				},
				{
					name: '商品评论',
					count: 0
				},
				{
					name: '推荐商品'
				}],
				goodsInfo:{},
				current: 0,
				commentList: [],
				goodsList: [],
				goodsId: null,
				isCollect: 0,
				cartCount: 0
			}
		},
		onLoad(option) {
			// console.log(option);
			this.goodsId = option.id
			
			this.getData()//商品数据
			this.getCartCount()//购物车数量
		},
		methods: {
			async getData(){
				const res = await this.$u.api.getgoodsInfo(this.goodsId)
				 console.log(res);
				 this.goodsInfo = res.goods
				 this.list[1].count = res.goods.comments.length
				 this.commentList =res.goods.comments
				 this.goodsList = res.like_goods
				 this.isCollect = res.goods.is_collect
				 console.log(this.isCollect);
			},
			change(index){
				this.current = index
			},
			//收藏商品
			async collect(){
				// 收藏商品Api
				await this.$u.api.getgoodsCollect(this.goodsId)
				
				if(this.isCollect==0){
					this.$u.toast('收藏成功！')
					this.isCollect=1
				}else{
					this.$u.toast('取消收藏')
					this.isCollect=0
				}
			},
			//添加到购物车
			async addCarts(){
				const params = {
					goods_id: this.goodsId
				}
				await this.$u.api.cartAdd(params)
				//提示消息
				this.$u.toast('加入购物车成功')
				//重新获取购物车数据
				this.getCartCount()
			},
			//获取购物车数量
			async getCartCount(){
				const token = this.vuex_token;
				//先判断是否登录
				if(token){
					const res = await this.$u.api.cartList()
					this.cartCount = res.data.length
				}
			},
			// 点赞
			getLike(index) {
				this.commentList[index].isLike = !this.commentList[index].isLike;
				if (this.commentList[index].isLike == true) {
					this.commentList[index].likeNum++;
				} else {
					this.commentList[index].likeNum--;
				}
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.top2{
		margin: 40rpx 0;
		font-size: 20px;
		font-weight: bold;
	}
	.des{
		.price{
			position: absolute;
			left: 30px !important;
			font-size: 18px;
			//border: 3px solid #73AD21;
			color: red;	
		}
		.sales{
			position: absolute;
			right: 50px!important;
			font-size: 15px;
			color: #888;
		}
	}
	.comment {
		display: flex;
		padding: 30rpx;
		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}
		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;
			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;
				.name {
					color: #5677fc;
				}
				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;
					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}
				.highlight {
					color: #5677fc;
					.num {
						color: #5677fc;
					}
				}
			}
			.content {
				margin-bottom: 10rpx;
			}
			.reply-box {
				background-color: rgb(242, 242, 242);
				border-radius: 12rpx;
				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;
					.username {
						font-size: 24rpx;
						color: #999999;
					}
				}
				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;
					.more {
						margin-left: 6rpx;
					}
				}
			}
			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;
				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}
			}
		}
	}
.navigation {
	display: flex;
	margin-top: 100rpx;
	border: solid 2rpx #f2f2f2;
	background-color: #ffffff;
	padding: 16rpx 0;
	.left {
		display: flex;
		font-size: 20rpx;
		.item {
			margin: 0 30rpx;
			&.car {
				text-align: center;
				position: relative;
				.car-num {
					position: absolute;
					top: -10rpx;
					right: -10rpx;
				}
			}
		}
	}
	.right {
		display: flex;
		font-size: 28rpx;
		align-items: center;
		.btn {
			line-height: 66rpx;
			padding: 0 30rpx;
			border-radius: 36rpx;
			color: #ffffff;
		}
		.cart {
			background-color: #ed3f14;
			margin-right: 30rpx;
		}
		.buy {
			background-color: #ff7900;
		}
	}
}
</style>
